home *** CD-ROM | disk | FTP | other *** search
- Rem Towers of Hanoi
- Rem By Richard The Rappa
-
- Rem Set the number of rings
- rings = 3
-
- Dim pin(rings+1,3)
-
- Dim pinx(3)
- Dim piny(3)
- Dim ringcolor(10)
-
- Rem Do instructions
- Gosub Instructions
-
- Cls
- Color 235
- Background "Peg"
-
- Rem Read in ring colors and coordinate data
- for clr = 1 to 10
- Read ringcolor(clr)
- next clr
-
- For t = 1 to 3
- Read pinx(t)
- Read piny(t)
- Next t
-
- data 126,94,54,64,70,80,40,104,113,30
- data 65,150,165,150,265,150
-
- Rem Initialize the pins
- for t = 1 to rings
- pin(rings - t+1, 1) = t
- next t
-
- turns = 0
- first = 0
- second = 0
- result = 0
-
- Rem Main Loop
- While pin(rings,3) = 0
- Rem Draw rings
- Gosub Update
- Rem Get player move
- Gosub GetMove
- Rem Move the rings
- Gosub RingMove
- if result = 0 then
- Rem There was an error
- Sound "GameShow"
- TextColor 126
- Rem Show the error message
- Position 20-Len(error$)/2,12
- Print error$
- sleep 15
- Rem Remove the error message
- Position 0,12
- Print " "
-
- else
- turns = turns + 1
- endif
- Wend
- Gosub Update
- a$ = "You won in "+str$(turns)+" moves"
- Sound "Victory"
- Banner a$
- End
-
- Rem Get first and second peg for movement
- Rem from user keypress of 1, 2, or 3
- Rem or 'Q' to quit game
- GetMove:
- Rem do this twice
- for t = 1 to 2
- Rem Prompt for first peg
- If t = 1 then
- TextColor 173
- Home
- Print
- Print " Press Source "
- else
- Rem Prompt for second peg
- TextColor 173
- Home
- Print
- Print " Press Destination"
- Endif
- Rem Get the keystroke
- a$ = ""
- While a$ <> "1" and a$ <> "2" and a$ <> "3" and a$ <> "q"
- a$ = inkey$
- Wend
- if a$ = "q" then end
- if a$ = "1" then sel = 1
- if a$ = "2" then sel = 2
- if a$ = "3" then sel = 3
- Rem If getting first peg
- if t = 1 then
- first = sel
- else
- Rem Getting second peg
- second = sel
- endif
- next t
-
-
- Rem Update the pictures for all the rings on the pegs
- Update:
-
- Background "Peg"
-
- xadder = 20 / rings
- yadder = 100 / rings
-
- For t = 1 to 3
-
- Rem Draw the rings over that peg
- For z = rings to 1 step -1
- if pin(z,t) <> 0 then
- clr = ringcolor(pin(z,t))
- x1 = pinx(t) - 10 - (xadder*pin(z,t))
- x2 = pinx(t) + 10 + (xadder*pin(z,t))
- y1 = piny(t) - (yadder * z) - 20
- y2 = piny(t) - (yadder * z) - 10
- Gosub RenderObject
- endif
- Next z
- Next t
-
- return
-
- Rem Check to see if a ring can be moved, and if so, move it
- RingMove:
- choice1 = 0
- index1 = 1
- choice2 = 0
- index2 = 1
- result = 0
-
- Rem Find the top ring of the source peg
- for z = 1 to rings
- if pin(z,first) <> 0 then
- choice1 = pin(z,first)
- index1 = z
- endif
- next z
-
- Rem Find the top ring of the destination peg
- for z = 1 to rings
- if pin(z,second) <> 0 then
- choice2 = pin(z,second)
- index2 = z
- endif
- next z
-
- Rem If the next pin is empty, put the ring there
- if choice2 = 0 then
- pin(index2,second) = pin(index1,first)
- pin(index1,first) = 0
- result = 1
- else
- Rem If the topmost ring is bigger than the ring we're moving, place the ring
- if choice2 > choice1 then
- pin(index2+1,second) = pin(index1,first)
- pin(index1,first) = 0
- result = 1
- endif
- endif
- Rem If we couldn't place the ring, say why
- If result = 0 Then
- If first = second then
- error$ = "You must choose two different pegs!"
- Else
- error$ = "Rings on top must be smaller!"
- Endif
- Endif
- return
-
- Rem Render a ring
- RenderObject:
- increment = 0
- for yy = y1 to y2
- Color clr + increment
- increment = increment + .5
- Line x1,yy to x2,yy
- next yy
- return
-
- Instructions:
- Cls
- color 235
- FillRect 0,0 to 320,240
- TextColor 95
- Print "Towers Of Hanoi"
- Print
- TextColor 21
- Print "The object is to move all the rings from"
- Print "Peg 1 onto Peg 3. You can move the rings"
- Print "between any two pegs, but you can NEVER"
- Print "put a larger ring over a smaller one."
- Print
- TextColor 156
- Print "Press 1,2, or 3 to select the top ring"
- Print "from the chosen peg, then press 1,2, or"
- Print "3 again to select where it's going."
- Print
- Print "Press 'Q' at any time to quit."
- Print
- TextColor 166
- Print "Press a key when ready"
- While inkey$ = ""
- Wend
- return
-
-
-
-
-